package com.ruoyi.passenger.order.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.management.basics.service.ITEmployeeService;
import com.ruoyi.management.basics.vo.GetDriverDetailVo;
import com.ruoyi.passenger.order.domain.TOrder;
import com.ruoyi.passenger.order.mapper.TOrderMapper;
import com.ruoyi.passenger.order.ro.GetHistoricalOrderParam;
import com.ruoyi.passenger.order.service.ITOrderService;
import com.ruoyi.passenger.order.vo.GetHistoricalOrderVO;
import com.ruoyi.passenger.order.vo.GetHistoricalOrderVOCopy;
import com.ruoyi.passenger.order.vo.GetOrderLocationVO;
import com.ruoyi.passenger.user.service.PassengerUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

/**
 * 订单Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-01
 */
@Service
public class TOrderServiceImpl implements ITOrderService {
    @Resource
    private TOrderMapper tOrderMapper;
    @Autowired
    private ITEmployeeService employeeService;
    @Autowired
    private PassengerUserService passengerUserService;

    /**
     * 历史订单
     *
     * @param param
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetHistoricalOrderVOCopy> selectHistoricalOrder(GetHistoricalOrderParam param,String userName) {
        if(StringUtils.isEmpty(userName)){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        GetDriverDetailVo user = employeeService.getEmployeeDetailByPhone(userName);
        if(null == user){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        if(null == user.getId()){
            throw new ServiceException("获取当前用户失败，请稍后重试");
        }
        param.setDriverId(user.getId().intValue());
        List<GetHistoricalOrderVOCopy> voList = tOrderMapper.selectTOrderByPlatformId(param);
        for (GetHistoricalOrderVOCopy vo:voList) {
            vo.setOrderDateDate(new SimpleDateFormat("yyyy-MM-dd").format(vo.getOrderDate()));
            vo.setOrderDateTime(new SimpleDateFormat("HH:mm:ss").format(vo.getOrderDate()));
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<GetHistoricalOrderVOCopy> selectHistoricalOrderAndPassengerId(GetHistoricalOrderParam param,String userName) {
        String platFormId = param.getPlatformId();
        if(null == platFormId  || "".equals(platFormId)){
            if(StringUtils.isEmpty(userName)){
                throw new ServiceException("获取当前用户失败，请稍后重试");
            }
            Long userId = passengerUserService.getUserId(userName);
            if(null == userId){
                throw new ServiceException("获取当前用户失败，请稍后重试");
            }
            param.setDriverId(userId.intValue());
        }
        List<GetHistoricalOrderVOCopy> voList = tOrderMapper.selectTOrderByPlatformIdAndPassengerId(param);
        for (GetHistoricalOrderVOCopy vo:voList) {
            vo.setOrderDateDate(new SimpleDateFormat("yyyy-MM-dd").format(vo.getOrderDate()));
            vo.setOrderDateTime(new SimpleDateFormat("HH:mm:ss").format(vo.getOrderDate()));
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrder(TOrder order) {
        tOrderMapper.addOrder(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public TOrder getOrderByPayOrderId(String payOrderId) {
        return tOrderMapper.selectOrderByPayOrderId(payOrderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<TOrder> getOrderListWithNoSync() {
        return tOrderMapper.selecttOrderListWithNoSync();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updatePayStatus(String payOrderId, String statusCode) {
        return tOrderMapper.updatePayStatus(payOrderId,statusCode);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<String> getPayOrderId() {
        return tOrderMapper.getPayOrderId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrder(TOrder order) {
        tOrderMapper.updateOrder(order);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateOrderList(List<TOrder> list) {
        tOrderMapper.updateOrderList(list);
    }

    @Override
    public void updateOrderAddressList(List<GetOrderLocationVO> list) {
        tOrderMapper.updateOrderAddressList(list);
    }

    @Override
    public List<GetOrderLocationVO> getAddress() {
        return tOrderMapper.getAddress();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateBatchPayStatus(List<Map> payList) {
        tOrderMapper.updateBatchPayStatus(payList);
    }

}
